#include "const.h"
#include <iostream>
#include <thread>
#include "boost/asio.hpp"
#include "json/json.h"
#include <signal.h>
#include <chrono>
#include <vector>


using boost::asio::ip::tcp;
bool b_stop_=false;
void SingalHandler(int signum)
{

    b_stop_=true;

}


//构建两个线程，一个用于读取数据，一个用于发送数据

std::vector<std::thread> threads;
int main()
{
    signal(SIGINT,SingalHandler);
    signal(SIGTERM,SingalHandler);
    auto start_time = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 100; i++)
    {
        threads.emplace_back([](){
            try
            {
                 boost::asio::io_context io_context;
                boost::asio::ip::tcp::socket socket(io_context);
                socket.connect(tcp::endpoint(boost::asio::ip::address::from_string("192.168.248.171"), 8888));
                //同步发送数据
                int i =0;
                while (i<500)
                {

                    Json::Value val;
                    short msg_id=MSG_HELLO_WORLD;
                    msg_id = boost::asio::detail::socket_ops::host_to_network_short(msg_id);

                    val["id"]=MSG_HELLO_WORLD;
                    val["data"]="hsl 666";
                    std::string ret_str = val.toStyledString();
                    const char * sendData=ret_str.c_str();
                    char sendBuf[MAX_LENGTH]={0};
                    short len = boost::asio::detail::socket_ops::host_to_network_short(strlen(sendData));
                    //前两个字节为ID
                    memcpy(sendBuf,&msg_id,HEAD_ID_LEN);
                    //2-4字节为消息体长度
                    memcpy(sendBuf+HEAD_ID_LEN,&len,HEAD_DATA_LEN);
                    //后续的字节为消息体
                    memcpy(sendBuf+HEAD_TOTAL_LEN,sendData,strlen(sendData));


                    boost::asio::write(socket,boost::asio::buffer(sendBuf,HEAD_TOTAL_LEN+strlen(sendData)));
                    std::this_thread::sleep_for(std::chrono::milliseconds(10));

                    std::cout<<">>begin to receive data+++++++++++++++++\n";

                    char receiveHead[HEAD_TOTAL_LEN]={0};
                    boost::asio::read(socket,boost::asio::buffer(receiveHead,HEAD_TOTAL_LEN));
                    //0-2字节为消息ID
                    short msgid=0;
                    memcpy(&msgid,receiveHead,HEAD_ID_LEN);
                    msgid=boost::asio::detail::socket_ops::network_to_host_short(msgid);
                    //2-4字节为消息体长度
                    short msgLen=0;
                    memcpy(&msgLen,receiveHead+HEAD_ID_LEN,HEAD_DATA_LEN);
                    msgLen=boost::asio::detail::socket_ops::network_to_host_short(msgLen);
                    char msg[MAX_LENGTH]={0};
                    boost::asio::read(socket,boost::asio::buffer(msg,msgLen));
                    std::cout<<"recv msg len = "<<msgLen<<std::endl;
                    std::cout<<"------------解析消息---------------\n";
                    Json::Reader reader;
                    Json::Value val2;
                    if(reader.parse(msg,val2))
                    {
                        std::cout<<">>id = "<<val2["id"].asInt()<<std::endl;
                        std::cout<<">>data = "<<val2["data"].asString()<<std::endl;
                    }


                    i++;
                }
                
            }
            catch(const std::exception& e)
            {
                std::cerr << e.what() << '\n';
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        });
    }
    
    //计算结束时间
    
    for (auto& thread : threads)
    {
        thread.join();
    }
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time);
    std::cout << "Total time: " << duration.count() << " s" << std::endl;
    
    return 0;

}
